<script setup lang="ts">
import { showLoadingToast, showToast } from 'vant'
import { ref } from 'vue'
import type { Image } from '@/types/consult'
import { uploadImg } from '@/api/consult'
defineProps<{
  disabled: boolean
}>()
const emit = defineEmits<{
  (e: 'send-text', value: string): void
  (e: 'send-image', value: Image): void
}>()
const text = ref('')
const sendText = () => {
  if (!text.value) return showToast('必须输入内容')

  emit('send-text', text.value)

  text.value = ''
}

const afterRead = async (data: any) => {
  if (!data.file) return
  const t = showLoadingToast('图片上传中...')
  const res = await uploadImg(data.file)
  t.close()

  // 传递给父组件
  emit('send-image', res.data)
}
</script>

<template>
  <div class="room-action">
    <van-field
      type="text"
      class="input"
      :border="false"
      placeholder="问医生"
      autocomplete="off"
      v-model="text"
      @keyup.enter="sendText"
      :disabled="disabled"
    ></van-field>
    <van-uploader :preview-image="false" :disabled="disabled" :after-read="afterRead">
      <cp-icon name="consult-img" />
    </van-uploader>
  </div>
</template>

<style lang="scss" scoped>
.room-action {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 60px;
  background-color: #fff;
  display: flex;
  align-items: center;
  padding: 0 16px;
  z-index: 1;
  box-sizing: border-box;
  .input {
    background-color: var(--cp-bg);
    border: none;
    border-radius: 25px;
    margin-right: 10px;
    padding: 8px 15px;
  }
  .cp-icon {
    width: 24px;
    height: 24px;
  }
}
</style>
